﻿jQuery(function($)
{
    var gridimgpath =  siteRoot + '/javascript/jQuery/jqGrid/themes/basic/images';
    $("#prodid").val(prodId);
   
//    $('#StatusBar').jnotifyInizialize({});
    $("button").button();
    
//======= Add new Photo

    $("#createBtn").click(function()
    {
        $("#dialogNewPhoto").dialog("open");
    });
    
    $("#dialogNewPhoto").dialog({
        resizable: true,
        autoOpen: false,
        height: 280,
        width:620,
        modal: true,
        overlay: { backgroundColor: '#000', opacity: 0.5 }
     });  
     
     $("#cancelDlg").click(function() {$("#dialogNewPhoto").dialog("close"); });
     
    $("#SendPic").click(function()
    {
        $("#LogoForm")
        .ajaxSubmit(
            {url: siteRoot + '/WS/NewPhoto.rails', 
            type: 'POST', 
            clearForm: true, 
            dataType: 'json',
            success: function(responseText, statusText, set)
                {
                    $('#StatusBar').jnotifyAddMessage({
                        text: 'Production photo added',
                        permanent: false,
                        type: 'message'
                        });
                    $('#tblPhotos').trigger("reloadGrid");
                }
            });
        $("#dialogNewPhoto").dialog("close");
        return false;                 
    });

//======= List & Edit existing photos
    var lastSel = -1;
        
    $('#tblPhotos').jqGrid({
        url: siteRoot + '/WS/GetPhotosJson.rails',
        postData: { id: prodId},
        datatype: "json",
        colNames: ['PhotoId', 'Photo', 'Caption'],
        jsonReader: { repeatitems: false },
        colModel:
        [
            { name: 'ID', key: true, hidden: true, editable: false },
            { name: 'Filename', index: 'Filename', width:70, hidden: false, editable: false, formatter:photoFormatter },
            { name: 'Caption', index: 'Caption', width: 700, editable: true, edittype:'textarea', editoptions: {rows: "5", cols: "60"} }
        ],
        editurl: siteRoot + '/WS/EditPhotoJson.rails',
        height: '100%',
        imgpath: gridimgpath,
        pager: $('#pager'),
        pgbuttons: false,
        pginput: false,
        toppager: true,
        viewrecords: false
    })
    .navGrid('#tblPhotos_toppager', { edit: true, edittitle: "Edit selected caption", 
                          add: false,
                          del: true, deltitle: "Remove Photo",
                          view:false, search: false },       // button options
                    {
                      url: siteRoot + '/WS/EditPhotoJson.rails',
                      closeAfterEdit: true,
                      width: 600
                    }, // edit options
                    {}, // add options
                    { msg: "Delete selected photo & caption?", 
                      url: siteRoot + '/WS/DeletePhotoJson.rails',
                    }  // del options
            );

    $("#waiting").slideUp("slow");
    $("#wizard").slideDown("slow");

    function photoFormatter(cellvalue, options, rowObject)
    {
        return '<img src="' + siteRoot+'/Show/'+cellvalue+'/60x60/Image.rails" />';
    }
});
